package com.yilian.xlmusic.android.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator;
import com.yilian.xlmusic.android.utils.LoadingAnimator;
import com.yilian.xlmusic.android.utils.net.LogInterceptor;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import rxhttp.HttpSender;
import rxhttp.RxHttp;
import rxhttp.wrapper.ssl.HttpsUtils;

import static rxhttp.wrapper.ssl.HttpsUtils.getSslSocketFactory;

public class MyApplication extends Application {
    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
//                layout.setPrimaryColorsId(R.color.color_FFAD0005, android.R.color.white);//全局设置主题颜色
                ClassicsHeader classicsHeader = new ClassicsHeader(context);
//                classicsHeader.setBackgroundResource(R.mipmap.xiari);
                return classicsHeader;//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new LoadingAnimator(context);
            }
        });
    }

    private static Context context;
    private static Map<String, Activity> destoryMap = new HashMap<>();

    public MyApplication() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();


        //日志初始化
        Logger.addLogAdapter(new AndroidLogAdapter());

        //初始化RxHttp
        if(!HttpSender.isInit()){
            RxHttp.setDebug(true);
            RxHttp.init(getDefaultOkHttpClient(),true);
        }

    }
    private static OkHttpClient getDefaultOkHttpClient() {
        HttpsUtils.SSLParams sslParams = getSslSocketFactory();
        return new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager) //添加信任证书
                .hostnameVerifier((hostname, session) -> true) //忽略host验证
                .addNetworkInterceptor(new LogInterceptor())//添加日志拦截器
                .build();
    }


    public static Context getContext() {
        return context;
    }

    /**
     * 添加到销毁队列
     *
     * @param activity 要销毁的activity
     */

    public static void addDestoryActivity(Activity activity, String activityName) {
        destoryMap.put(activityName, activity);
    }

    /**
     * 销毁指定Activity
     */
    public static void destoryActivity(String activityName) {
        Set<String> keySet = destoryMap.keySet();
        for (String key : keySet) {
            if (key.equals(activityName)) {
                Objects.requireNonNull(destoryMap.get(key)).finish();
            }
        }
    }

    /**
     * 销毁所有Activity
     */
    public static void destroyAllActivity() {
        Set<String> keySet = destoryMap.keySet();
        for (String key : keySet) {
            Objects.requireNonNull(destoryMap.get(key)).finish();
        }
    }

}
